package com.starry.home.ui.hospital

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayout
import com.starry.core.base.BaseActivity
import com.starry.core.constant.GlobalConfig
import com.starry.core.utils.SpUtil
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivityDoctorBinding
import com.starry.home.logic.model.hospital.DateBean
import com.starry.home.logic.model.hospital.DoctorInfoBean
import com.starry.home.logic.model.hospital.RegisterBean
import com.starry.home.utils.DateUtils
import com.starry.home.viewmodel.hospital.DoctorViewModel
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Date
import kotlin.properties.Delegates


/**
 * Package:com.starry.dotpen_patients.ui.hospital
 * Author:starr
 * Time:2024/1/7 19:41
 * Description: 医生时间列表
 */
@RequiresApi(Build.VERSION_CODES.O)
class DoctorActivity : BaseActivity<ActivityDoctorBinding, DoctorViewModel>() {


    private lateinit var doctorBean: DoctorInfoBean
    private lateinit var registerBean: RegisterBean
    private var dates = mutableListOf<DateBean>()
    private var pos by Delegates.notNull<Int>()

    companion object {
        var ysId by Delegates.notNull<Int>()
        fun startAction(context: Context, bundle: Bundle) {
            val intent = Intent(context, DoctorActivity::class.java)
            ysId = bundle.getInt("ysId")
            context.startActivity(intent)
        }
    }


    @OptIn(DelicateCoroutinesApi::class)
    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n")
    override fun initData() {

        GlobalScope.launch {
            doctorBean = mViewModel.getYsInfo(ysId)
            if (doctorBean.msg == "success") {
                withContext(Dispatchers.Main) {
                    Glide.with(getContext()).load(doctorBean.emrYsInfo.emrYsImg)
                        .into(mDataBinding.ivHead)
                    mDataBinding.tvDoctorTitle.text = doctorBean.emrYsInfo.emrYsName
                    mDataBinding.tvDoctor.text =
                        "${doctorBean.emrYsInfo.emrYsName} ${doctorBean.emrYsInfo.emrYsTitle} ${doctorBean.emrYsInfo.emrYsTitle}"

                }
            } else {
                withContext(Dispatchers.Main) {
                    Toast.makeText(getContext(), doctorBean.msg, Toast.LENGTH_SHORT).show()
                }
            }

        }

    }

    @SuppressLint("SetTextI18n")
    @OptIn(DelicateCoroutinesApi::class)
    @RequiresApi(Build.VERSION_CODES.O)
    override fun initView() {
        mDataBinding.titleTopBar.baseTvTitle.text = "医生详情"
        mDataBinding.titleTopBar.baseIvBack.setOnClickListener {
            finish()
        }
        mDataBinding.titleTopBar.baseIvBack.visibility = View.VISIBLE
        ZKStatusBarUtil.setTransparentForWindow(this)
        ZKStatusBarUtil.setDarkMode(this)
        dates.add(DateUtils.dateWeek(0))
        dates.add(DateUtils.dateWeek(1))
        dates.add(DateUtils.dateWeek(2))
        dates.add(DateUtils.dateWeek(3))
        dates.add(DateUtils.dateWeek(4))

        for (i in 0..<mDataBinding.tabLayout.tabCount) {
            val tab = mDataBinding.tabLayout.getTabAt(i)
            tab?.text = "${dates[i].week}\n${dates[i].day}"
        }

        val tabZero = mDataBinding.tabLayout.getTabAt(0)
        tabZero?.text = "${dates[0].week}\n${dates[0].day}"
        mDataBinding.before.tvTime.text = "${dates[0].day} ${dates[0].week} 上午"
        mDataBinding.after.tvTime.text = "${dates[0].day} ${dates[0].week} 下午"
        pos = 0
        mDataBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            @SuppressLint("SetTextI18n")
            @RequiresApi(Build.VERSION_CODES.O)
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.text = "${dates[tab?.position!!].week}\n${dates[tab.position].day}"
                when (tab.position) {
                    0 -> {
                        mDataBinding.before.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 上午"
                        mDataBinding.after.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 下午"
                        pos = tab.position
                    }

                    1 -> {
                        mDataBinding.before.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 上午"
                        mDataBinding.after.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 下午"
                        pos = tab.position
                    }

                    2 -> {
                        mDataBinding.before.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 上午"
                        mDataBinding.after.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 下午"
                        pos = tab.position
                    }

                    3 -> {
                        mDataBinding.before.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 上午"
                        mDataBinding.after.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 下午"
                        pos = tab.position
                    }

                    else -> {
                        mDataBinding.before.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 上午"
                        mDataBinding.after.tvTime.text =
                            "${dates[tab.position].day} ${dates[tab.position].week} 下午"
                        pos = tab.position
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })

        val userId = SpUtil.getIntValue(GlobalConfig.userId)
        if (userId < 0) {
            Toast.makeText(getContext(), "当前未登录，无法挂号", Toast.LENGTH_SHORT).show()
        } else {
            mDataBinding.before.btRegister.setOnClickListener {
                GlobalScope.launch {
                    registerBean = RegisterBean(
                        emrHzId = SpUtil.getIntValue(GlobalConfig.userId),
                        emrYsId = ysId,
                        emrGhState = 0,
                        emrGhOrder = DateUtils.date2String(Date(), DateUtils.yyyyMMddHHmmSS.get()),
                        emrGhTime = "${dates[pos].yyyyMMddHHmmSS} 10:00:00"
                    )
                    val result = mViewModel.saveRegister(registerBean)
                    if (result.msg == "success") {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(
                                getContext(),
                                "挂号成功，就诊时间${registerBean.emrGhTime}",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } else {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(getContext(), result.msg, Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }
            mDataBinding.after.btRegister.setOnClickListener {
                GlobalScope.launch {
                    registerBean = RegisterBean(
                        emrHzId = SpUtil.getIntValue(GlobalConfig.userId),
                        emrYsId = ysId,
                        emrGhState = 0,
                        emrGhOrder = DateUtils.date2String(Date(), DateUtils.yyyyMMddHHmmSS.get()),
                        emrGhTime = "${dates[pos].yyyyMMddHHmmSS} 14:00:00"
                    )
                    val result = mViewModel.saveRegister(registerBean)
                    if (result.msg == "success") {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(
                                getContext(),
                                "挂号成功，就诊时间${registerBean.emrGhTime}",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } else {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(getContext(), result.msg, Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }
        }


    }


    override fun getLayoutId() = R.layout.activity_doctor
}